package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.config.SystemInitialization;
import com.ayf.payment.game.api.dto.MerchantAlipayConfigDTO;
import com.ayf.payment.game.api.dto.MerchantConfigDTO;
import com.ayf.payment.game.api.dto.criteria.PartitionCriteria;
import com.ayf.payment.game.api.dto.criteria.SettleConfigCriteria;
import com.ayf.payment.game.api.dto.criteria.SettleCycleCriteria;
import com.ayf.payment.game.api.dto.platform.SettleConfigDTO;
import com.ayf.payment.game.api.entity.Merchant;
import com.ayf.payment.game.api.entity.MerchantConfig;
import com.ayf.payment.game.api.exception.BusinessException;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.MerchantConfigMapper;
import com.ayf.payment.game.api.mapper.MerchantMapper;
import com.ayf.payment.game.api.mapper.MerchantProxyRelMapper;
import com.ayf.payment.game.api.mapper.MerchantSettleRateCustomMapper;
import com.ayf.payment.game.api.mapper.platform.MerchantSettleRateMapper;
import com.ayf.payment.game.api.service.MerchantAlipayConfigService;
import com.ayf.payment.game.api.service.MerchantConfigService;
import com.ayf.payment.game.api.service.MerchantRepository;
import com.ayf.payment.game.api.service.platform.SettleConfigService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.type.YesAndNo;
import com.ayf.payment.game.api.utils.Md5Utils;
import com.ayf.payment.game.api.utils.SnowFlakeUtils;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
@Transactional(rollbackFor = Throwable.class)
public class MerchantConfigServiceImpl implements MerchantConfigService {

    @Resource
    private MerchantConfigMapper merchantConfigMapper;
    @Resource
    private MerchantRepository merchantRepository;

    @Autowired
    private SettleConfigService settleConfigService;
    @Autowired
    private SystemInitialization systemInitialization;

    @Resource
    private MerchantMapper merchantMapper;

    @Resource
    private MerchantProxyRelMapper merchantProxyRelMapper;

    @Resource
    private MerchantSettleRateCustomMapper merchantSettleRateCustomMapper;

    @Resource
    private MerchantSettleRateMapper merchantSettleRateMapper;
    @Resource
    private MerchantAlipayConfigService merchantAlipayConfigService;

    @Override
    public MerchantConfig selectOne(Integer merchantId) {
        if (merchantId == null) {
            throw new ParameterException("商户号不能为空");
        }
        MerchantConfig merchantConfig = new MerchantConfig();
        merchantConfig.setMerchantId(merchantId);
        return merchantConfigMapper.selectOne(merchantConfig);
    }

    @Override
    public MerchantConfigDTO getMerchantConfigDTO(Integer merchantId) {
        MerchantConfigDTO merchantConfigDTO=merchantConfigMapper.getMerchantConfigDTO(merchantId);
        Integer settleCycle;
        if (YesAndNo.YES.equals(merchantConfigDTO.getSettleCycleCustom())) {
            settleCycle = merchantConfigDTO.getSettleCycle();
        } else {
            SettleConfigDTO settleConfigDTO = settleConfigService.getSettleConfig();
            settleCycle = settleConfigDTO.getSettleCycle();
        }
        merchantConfigDTO.setSettleCycle(settleCycle);
        if (StringUtils.isBlank(merchantConfigDTO.getUniqueId())) {
            String encode = Md5Utils.encode(SnowFlakeUtils.getUniqueId());
            merchantConfigDTO.setUniqueId(encode);
            MerchantConfig config = new MerchantConfig();
            config.setMerchantId(merchantConfigDTO.getMerchantId());
            config.setUniqueId(encode);
            merchantConfigMapper.updateConfigSelective(config);
        }
        return merchantConfigDTO;
    }

    @Override
    public boolean updateQQ(MerchantConfig merchantConfig) {
        return merchantConfigMapper.updateQQ(merchantConfig) > 0;
    }

    @Override
    public boolean updatePlayerIntegralStatus(MerchantConfig merchantConfig) {
        return merchantConfigMapper.updatePlayerIntegralStatus(merchantConfig) > 0;
    }

    @Override
    public boolean updatePlayerIntegralQq(MerchantConfig merchantConfig) {
        return merchantConfigMapper.updatePlayerIntegralQq(merchantConfig) > 0;
    }

    @Override
    public boolean updateMessageBoxStatus(MerchantConfig merchantConfig) {
        merchantRepository.del(merchantConfig.getMerchantId());
        return merchantConfigMapper.updateConfigSelective(merchantConfig) > 0;
    }

    @Override
    public boolean updateMinRechargeAmount(MerchantConfig merchantConfig) {
        merchantRepository.del(merchantConfig.getMerchantId());
        return merchantConfigMapper.updateMinRechargeAmount(merchantConfig) > 0;
    }

    @Override
    public boolean updateSettleIdentificationId(MerchantConfig merchantConfig) {
        if (merchantConfig.getSettleIdentificationId() == null) {
            throw new ParameterException("费率id不能为空");
        }
        if (merchantConfig.getMerchantId() == null) {
            throw new ParameterException("商户号不能为空");
        }
        merchantConfigMapper.updateConfigSelective(merchantConfig);
        Merchant merchant = merchantMapper.selectByPrimaryKey(merchantConfig.getMerchantId());
        merchantSettleRateCustomMapper.updateMerchantRateId(merchantConfig.getMerchantId(), null, merchantConfig.getSettleIdentificationId());
        if ("PROXY".equals(merchant.getMerchantType())) {
            Map<Integer, Map<Integer, BigDecimal>> map = merchantSettleRateMapper.getProxyGroupMaxSettleRateMapByMerchantId(merchantConfig.getMerchantId());
            Set<Integer> keySet = map.keySet();
            for (Integer key : keySet) {
                if (!map.isEmpty() && null != map.get(key) && (systemInitialization.settleRateMap().get(merchantConfig.getSettleIdentificationId()).get(key)).compareTo(map.get(key).get("paymentRate")) < 0) {
                    throw new ParameterException("费率更改失败" + map.get(key).get("payProductName")
                            + "费率不能低于代理分组商户最大费率[" + map.get(key).get("paymentRate") + "]");
                }
            }
            List<Integer> ids = merchantProxyRelMapper.selectLowerIds(merchantConfig.getMerchantId());
            if (ids != null && ids.size() > 0) {
                merchantConfigMapper.updateProxyIdentificationId(ids, merchantConfig.getSettleIdentificationId());
                for (Integer id : ids) {
                    merchantRepository.del(id);
                }
            }

        }
        merchantRepository.del(merchantConfig.getMerchantId());
        return true;
    }

    @Override
    public SettleConfigDTO selectConfigByPartitionId(String partitionId) {
        if (StringUtils.isBlank(partitionId)) {
            throw new BusinessException(CommonConstants.RETURN_ERROR, "分区id不能为空");
        }
        SettleConfigDTO settleConfig = settleConfigService.getSettleConfig();
        MerchantConfig merchantConfig = merchantConfigMapper.selectConfigByPartitionId(partitionId);
        if (merchantConfig.getMinRechargeAmount() != null) {
            settleConfig.setMinRechargeAmount(merchantConfig.getMinRechargeAmount());
        }
        if (StringUtils.isBlank(merchantConfig.getCustomerQq())) {
            settleConfig.setCustomerQq(merchantConfig.getCustomerQq());
        }
        return settleConfig;
    }

    @Override
    public boolean updateSettleCycle(SettleCycleCriteria settleCycle) {
        if (StringUtils.isBlank(settleCycle.getSettleCycleCustom())) {
            throw new ParameterException("是否平台结算周期不能为空");
        }
        if (settleCycle.getMerchantId() == null) {
            throw new ParameterException("商户ID不能为空");
        }
        merchantRepository.del(settleCycle.getMerchantId());

        if ("YES".equals(settleCycle.getSettleCycleCustom())) {
            if (settleCycle.getSettleCycle() == null) {
                throw new ParameterException("结算周期不能为空");
            }
            MerchantConfig merchantConfig = new MerchantConfig();
            merchantConfig.setMerchantId(settleCycle.getMerchantId());
            merchantConfig.setSettleCycleCustom(YesAndNo.YES);
            merchantConfig.setSettleCycle(settleCycle.getSettleCycle());
            merchantConfigMapper.updateConfigSelective(merchantConfig);
            return true;
        } else if ("NO".equals(settleCycle.getSettleCycleCustom())) {
            MerchantConfig merchantConfig = new MerchantConfig();
            merchantConfig.setMerchantId(settleCycle.getMerchantId());
            merchantConfig.setSettleCycleCustom(YesAndNo.NO);
            merchantConfigMapper.updateConfigSelective(merchantConfig);
            return true;
        }
        return false;
    }

    @Override
    public boolean updateSettleType(SettleConfigCriteria settleConfigCriteria) {
        if (settleConfigCriteria.getMerchantId() == null) {
            throw new ParameterException("商户id不能为空");
        }
        merchantRepository.del(settleConfigCriteria.getMerchantId());
        return merchantConfigMapper.updateSettleType(settleConfigCriteria)>0;
    }

    @Override
    public boolean updatePayStatus(SettleConfigCriteria settleConfigCriteria) {
        if (settleConfigCriteria.getMerchantId() == null) {
            throw new ParameterException("商户id不能为空");
        }
        if (settleConfigCriteria.getPayStatus() == null) {
            throw new ParameterException("商户代付状态不能为空");
        }
        Merchant merchant=new Merchant();
        merchant.setId(settleConfigCriteria.getMerchantId());
        merchant.setPhone(settleConfigCriteria.getPhone());
        merchant.setSmsStatus(settleConfigCriteria.getSmsStatus());
        merchant.setQq(settleConfigCriteria.getQq());
        merchant.setName(settleConfigCriteria.getName());
        merchantMapper.updateByPrimaryKeySelective(merchant);
        merchantConfigMapper.updatePayStatus(settleConfigCriteria);

        MerchantAlipayConfigDTO dto=new MerchantAlipayConfigDTO();
        dto.setMerchantId(settleConfigCriteria.getMerchantId());
        dto.setStatus(settleConfigCriteria.getAliPayStatus());
        merchantAlipayConfigService.updateConfig(dto);

        return true;
    }

    @Override
    public MerchantConfig selectMsgStatus(PartitionCriteria partitionCriteria) {
        return merchantConfigMapper.selectMsgStatus(partitionCriteria);
    }

    @Override
    public MerchantConfigDTO getMerchantConfigByPartitionId(String partitionId) {
        return BeanConvertUtils.convert(merchantConfigMapper.selectConfigByPartitionId(partitionId),MerchantConfigDTO.class);
    }

    @Override
    public boolean updateSkin(MerchantConfig merchantConfig) {
        return merchantConfigMapper.updateConfigSelective(merchantConfig) > 0;
    }

    @Override
    public MerchantConfigDTO getConfigByUniqueId(String uniqueId) {
        MerchantConfig config = new MerchantConfig();
        config.setUniqueId(uniqueId);
        return BeanConvertUtils.convert(merchantConfigMapper.selectOne(config), MerchantConfigDTO.class);
    }

    @Override
    public boolean updateMerchantConfig(MerchantConfigDTO merchantConfigDTO) {
        int num = merchantConfigMapper.updateConfigSelective(BeanConvertUtils.convert(merchantConfigDTO,MerchantConfig.class));
        merchantRepository.del(merchantConfigDTO.getMerchantId());
        return num>0?true:false;
    }
}

